package main

import (
	"fmt"
)

/*
若干个括号，求最长合规字串长度
*/
func dpCount(str string) int {
	var dp []int = make([]int, len(str))
	var max int
	for i := 1; i < len(str); i++ {
		if str[i] == ')' {
			pre := i - (dp[i-1] + 1)
			if pre >= 0 && str[pre] == '(' {
				dp[i] = 2 + dp[i-1] 
				if pre - 1 >= 0 {
					dp[i] += + dp[pre - 1]
				}
			}
		}
		max = Max(max, dp[i])

		s++
	}
	fmt.Println(dp)
	return max
}

func leftCount(str string) int {
	var max int
	for i := 0; i < len(str); i++ {
		var idx int = i
		var count int
		var length int
		for idx <= len(str)-1 {
			if str[idx] == '(' {
				count++
			} else {
				count--
				if count < 0 {
					break
				}
				length++
			}
			idx++

			s++
		}
		if count <= 0 {
			max = Max(max, length*2)
		}
		fmt.Print(" ", length*2)
	}
	return max
}

var s int

func main() {
	res := dpCount("(()())(((())()))))))()()(()())")
	fmt.Println(res)
	fmt.Println(s)
}

func Max(a int, b int) int {
	if a > b {
		return a
	}
	return b
}